package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import DBConn.DBConn;
import MySQL.User;

public class UserDaoImpl implements UserDao{
	
	//修改
	public void update(User u) {
		String ssql = "update user set password=? where id=?";
		Connection conn = DBConn.open();
		try {
			PreparedStatement pstmt = conn.prepareStatement(ssql);
			pstmt.setString(1,u.getPassword());
			pstmt.setString(2,u.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConn.close(conn);
		}
	}
	
	public User getUserById(String id) {
		String ssql = "select * from user where id=?";
		Connection conn = DBConn.open();
		try {
			PreparedStatement pstmt = conn.prepareStatement(ssql);
			pstmt.setString(1, id);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				String password = rs.getString(2);
				String type = rs.getString(3);
				User u = new User();
				u.setId(id);
				u.setPassword(password);
				u.setType(type);
				return u;
			}
		} catch (SQLException e){
			e.printStackTrace();
		}finally{
			DBConn.close(conn);
		}	
		return null;
	}
}
